package com.soda.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;

import com.soda.bean.Fmenu;
import com.soda.bean.Menu;
import com.soda.bean.Role;
import com.soda.bean.User;
import com.soda.dao.FmenuDao;
import com.soda.dao.MenuDao;
import com.soda.dao.RoleDao;
import com.soda.dao.UserDao;
import com.soda.dao.impl.FmenuDaoImpl;
import com.soda.dao.impl.MenuDaoImpl;
import com.soda.dao.impl.RoleDaoImpl;
import com.soda.dao.impl.UserDaoImpl;
import com.soda.service.UserService;
import com.soda.util.MyBatisUtil;
import com.soda.util.PageInfo;
import com.soda.util.SelectUserInfo;

public class UserServiceImpl implements UserService{

	UserDao ud=new UserDaoImpl();
	RoleDao rd=new RoleDaoImpl();
	MenuDao md=new MenuDaoImpl();
	FmenuDao fd=new FmenuDaoImpl();
	
	public User login(String loginname) {
		// TODO Auto-generated method stub
		SqlSession session =MyBatisUtil.getSession();
		User u=session.getMapper(UserDao.class).login(loginname);//登录
		if(u!=null){
		u.getRole().setRolId(u.getRoleId());
		System.out.println("====="+u.getRole().getRolId());
		Role r=session.getMapper(RoleDao.class).getRoleById(u.getRole().getRolId());   //根据角色id查找角色，并给user中的角色赋值
		
		List<Fmenu> fmenus=fd.getFmenuByRole(r.getRolId());//根据角色id查找父级菜单
	
		/*List<Menu> menus=new ArrayList<Menu>();
          for (Fmenu fmenu : fmenus) {  
        	  //通过roleid和fmenuid查找该用户所拥有的id//根据父级菜单id查找对应的子级菜单
			List<Menu> mes=md.getMenuByRoleIdFmenuId(r.getRolId(),fmenu.getFmeId());
			for (Menu menu : mes) {
				Menu me=md.getMenuById(menu.getMenId());
				menus.add(me);
			}
		     fmenu.setMenus(menus);//把查询到的子级菜单赋值给对应的父级菜单
		}*/
         
        List<Menu> menus=session.getMapper(MenuDao.class).getMenuByRoleId(r.getRolId());
        System.out.println(""+menus.get(0).getMenName());
        for(Menu m:menus){
        	m.getFmenu().setFmeId(m.getFmenu_id());
        }
        r.setMenus(menus);
		r.setFmenus(fmenus);//把父级菜单赋值给角色
		u.setRole(r);  //把角色赋值给用户
		MyBatisUtil.closeSession(session);
		return u;  //返回用户
		}
		return null;
	}
	
	public int editUserInfo(User u) {
		// TODO Auto-generated method stub
		SqlSession session =MyBatisUtil.getSession();
		int count=session.getMapper(UserDao.class).editUserInfo(u);
		if(count>0){
			session.commit();
		}
		MyBatisUtil.closeSession(session);
		return count;
	}
	public int editPassWord(int useId, String newpwd) {
		// TODO Auto-generated method stub
		SqlSession session =MyBatisUtil.getSession();
		int count=session.getMapper(UserDao.class).editPassWord(useId,newpwd);
		if(count>0){
			session.commit();
		}
		MyBatisUtil.closeSession(session);
		return count;
	}
	public List<User> getAllUser(SelectUserInfo sui, PageInfo pi) {
		SqlSession session =MyBatisUtil.getSession();
		pi.setStartIndex();
		Map<String,Object> map=new HashMap<String,Object>();
		if(sui!=null){
			map.put("loginname", sui.getLoginname());
			map.put("uname", sui.getUname());
			map.put("ustate", sui.getUstate());
		}
		if(pi!=null){
			map.put("startIndex", pi.getStartIndex());
			map.put("per", pi.getPer());
		}
		List<User> userlist=session.getMapper(UserDao.class).getAllUser(map);
		MyBatisUtil.closeSession(session);
		return userlist;
	}
	public User getUserById(int id) {
		SqlSession session =MyBatisUtil.getSession();
		User u=session.getMapper(UserDao.class).getUserById(id);
		System.out.println("+===="+u.getRole().getRolId());
		Role r=session.getMapper(RoleDao.class).getRoleById(u.getRole().getRolId());
		MyBatisUtil.closeSession(session);
		u.setRole(r);
		return u;
	}
	public int editUserInfoByAdmin(User u) {
		SqlSession session =MyBatisUtil.getSession();
		u.setRoleId(u.getRole().getRolId());
		int count=session.getMapper(UserDao.class).editUserInfoByAdmin(u);
		if(count>0){
			session.commit();
		}
		MyBatisUtil.closeSession(session);
		return count;
	}
	public int editUserState(int uid, int ustate) {
		SqlSession session =MyBatisUtil.getSession();
		int count=session.getMapper(UserDao.class).editUserState(uid,ustate);
		if(count>0){
			session.commit();
		}
		MyBatisUtil.closeSession(session);
		return count;
	}
	public int deleteById(int useId) {
		SqlSession session =MyBatisUtil.getSession();
		int count=session.getMapper(UserDao.class).deleteById(useId);
		if(count>0){
			session.commit();
		}
		MyBatisUtil.closeSession(session);
		return count;
	}
	public int getUserCount(SelectUserInfo sui) {
		SqlSession session =MyBatisUtil.getSession();
		int count=session.getMapper(UserDao.class).getUserCount(sui);
		MyBatisUtil.closeSession(session);
		return count;
	}
	public int addUser(User u) {
		SqlSession session =MyBatisUtil.getSession();
		u.setRoleId(u.getRole().getRolId());
		int count=session.getMapper(UserDao.class).addUser(u);
		if(count>0){
			session.commit();
		}
		MyBatisUtil.closeSession(session);
		return count;
	}
	public int ajaxSelectLoginName(String loginname) {
		// TODO Auto-generated method stub
		SqlSession session =MyBatisUtil.getSession();
		if(session.getMapper(UserDao.class).ajaxSelectLoginName(loginname)>0){
			MyBatisUtil.closeSession(session);
			return 1;
		}
		MyBatisUtil.closeSession(session);
		return 0;
	}
	public String getNameById(int id) {
		SqlSession session =MyBatisUtil.getSession();
		String name=session.getMapper(UserDao.class).getNameById(id);
		MyBatisUtil.closeSession(session);
		return name;
	}

}
